package io.netty.example.helloworld.server;

/***
 这里需要继承Netty中ChannelInitializer类，然后重写initChannel该方法，进行添加相应的设置，传输协议设置，以及相应的业务实现类。

*/
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    private static final StringDecoder DECODER = new StringDecoder();
    private static final StringEncoder ENCODER = new StringEncoder();
    private static final ServerHandler SERVER_HANDLER = new ServerHandler();

    @Override
    public void initChannel(SocketChannel ch) throws Exception{
        ChannelPipeline pipeline = ch.pipeline();
        //添加帧限定符来防止粘包现象
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        //解码和编码和和客户端一致
        pipeline.addLast(DECODER);
        pipeline.addLast(ENCODER);
        //业务逻辑的实现类
        pipeline.addLast(SERVER_HANDLER);


    }

}
